package com.cms.config;

import com.cms.filter.PageCacheFilter;
import com.cms.filter.PermissionFilter;
import com.cms.filter.UrlFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

    @Autowired
    private PageCacheFilter pageCacheFilter;

    @Autowired
    private PermissionFilter permissionFilter;

    @Autowired
    private UrlFilter urlFilter;

    @Bean
    public FilterRegistrationBean registerPageCacheFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(pageCacheFilter);
        registration.addUrlPatterns("/*");
        registration.setName("pageCacheFilter");
        registration.addInitParameter("cacheName","PageCacheFilter");
        registration.setOrder(1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean registerPermissionFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(permissionFilter);
        registration.addUrlPatterns("/admin/*");
        registration.setName("permissionFilter");
        registration.setOrder(2);
        return registration;
    }

    @Bean
    public FilterRegistrationBean registerUrlFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(urlFilter);
        registration.addUrlPatterns("/*");
        registration.setName("urlFilter");
        registration.setOrder(3);
        return registration;
    }

}
